<template>
  <div class="app-container">
    <div class="layout-content">
      <template v-if="model.sampleType == 'sample'">
        <van-cell title="样品编号" :value="model.sampleCode" />
        <van-cell title="样件名称" :value="model.sampleName" />
        <van-cell title="零部件号或发动机号" :value="model.sampleModel" />
        <van-cell title="生产厂家" :value="model.deliveryOrg" />
        <van-cell title="还样人" :value="model.applyPersonTxt" />
        <van-cell title="还样部门" :value="model.orgCodeTxt" />
        <van-cell title="关联委托单" v-model="model.projectId_dictText" />
        <van-field
          label="归还时间"
          placeholder="请选择归还时间"
          v-model="model.returnDate"
          @click="backdataShow = true"
          input-align="right"
          readonly
        />
        <!-- remark -->

        <!-- <van-field
          label="关联委托单"
          placeholder="请下拉选择委托单"
          v-model="model.projectCode"
          @click="projectShow = true"
          input-align="right"
          readonly
        /> -->
        <van-field
          label="备注"
          placeholder="请输入备注"
          v-model="model.remark"
          input-align="right"
        />
      </template>
      <template v-else>
        <van-cell title="样品编号" :value="model.sampleCode" />
        <van-cell title="车辆名称" :value="model.sampleName" />
        <van-cell title="车型" :value="model.carModel_dictText" />
        <van-cell title="VIN" :value="model.sampleModel" />
        <van-cell title="生产厂家" :value="model.deliveryOrg" />
        <van-cell title="还样人" :value="model.applyPersonTxt" />
        <van-cell title="还样部门" :value="model.orgCodeTxt" />
        <van-cell title="关联委托单" v-model="model.projectId_dictText" />

        <van-field
          label="归还时间"
          placeholder="请选择归还时间"
          v-model="model.returnDate"
          @click="backdataShow = true"
          input-align="right"
          readonly
        />
      </template>
      <van-row style="margin-top: 50px">
        <van-col span="12" style="text-align: center">
          <van-button type="info" @click="submit">确认</van-button>
        </van-col>
        <van-col span="12" style="text-align: center">
          <van-button @click="cancel" type="info">取消</van-button>
        </van-col>
      </van-row>
      <van-popup v-model="projectShow" round position="bottom">
        <van-picker
          title="关联委托单"
          show-toolbar
          :columns="sheet"
          @confirm="onConfirm"
          @cancel="onCancel"
        />
      </van-popup>
      <van-popup v-model="backdataShow" round position="bottom">
        <van-datetime-picker
          v-model="currentTime"
          type="date"
          title="选择年月日"
          @confirm="onContime"
          @cancel="onCantime"
        />
      </van-popup>
    </div>
    <div class="layout-footer">
      <TabBar :activeTab="2"></TabBar>
    </div>
  </div>
</template>

<script>
import TabBar from "@/components/TabBar";
import { Notify } from "vant";
import dayjs from "dayjs";
import { getAction, postAction } from "@/api/manage";
export default {
  data() {
    return {
      sheet: [],
      currentTime: "",
      projectShow: false,
      backdataShow: false,
      model: {
        applyPerson: "",
        orgCodeTxt: "",

        remark: "",
        projectCode: "",
      },

      url: {
        detail: "activitiProcess/queryProcessByKey",
        ProcessNodeList: "actProcessIns/queryProcessNodeList",
        apply: "actBusiness/apply",
        samplelist: "/lims/limsProjectSample/queryById",
        queryProjectBySampleId:
          "lims/limsProjectSampleOrder/queryProjectBySampleId",
      },
      params: {
        assignees: "",
        procDeTitle: "",
        procDefId: "",
        processKey: this.$route.query.processKey,
        tableName: "",
      },
    };
  },
  created() {
    //获取流程信息
    this.getDetail();
    //获取流程人员
    this.getProcessNodeList();
    //获取样品信息
    this.getSampleDetial();
    //获取委托单
    this.getqueryProjectBySampleId();
  },
  methods: {
    onCantime() {
      this.backdataShow = false;
    },
    onContime() {
      console.log(this.currentTime);

      this.model.returnDate = dayjs(this.currentTime).format("YYYY-MM-DD");
      this.backdataShow = false;
      console.log(this.model.returnDate);
    },
    //还样取消
    cancel() {
      this.$router.go(-1);
    },
    //还样提交
    submit() {
      let projectSampleOrderItemAddRequestList = [
        {
          sampleId: this.model.sampleId || this.model.id,
          sampleName: this.model.sampleName,
        },
      ];
      if (this.model.returnDate) {
        projectSampleOrderItemAddRequestList[0].returnDate = dayjs(
          this.model.returnDate
        ).format("YYYY-MM-DD hh:mm:ss");
      }
      this.params.formData = {
        applyOrg: this.model.applyOrg,
        applyPerson: this.model.applyPerson,
        applyType: "return",
        // saveRoute: "/sample/borrow/list",
        projectSampleOrderItemAddRequestList:
          projectSampleOrderItemAddRequestList,
      };
      if (this.model.projectId) {
        this.params.formData.projectId = this.model.projectId;
      }
      if (this.model.remark) {
        this.params.formData.remark = this.model.remark;
      }

      // projectId: this.model.projectId,
      // this.params.projectSampleOrderItemAddRequestList =
      //   projectSampleOrderItemAddRequestList;
      console.log(this.params);

      postAction(this.url.apply, this.params).then((res) => {
        if (res.success) {
          Notify({ type: "sucesss", message: res.message });
        } else {
          Notify({ type: "warning", message: res.message });
        }
      });
    },
    //点击确认
    onConfirm(val) {
      this.projectShow = false;
      console.log(val);
      this.model.projectCode = val.projectCode;
      this.model.projectId = val.id;
    },
    //点击取消
    onCancel() {
      this.projectShow = false;
    },
    getqueryProjectBySampleId() {
      let params = {
        id: this.$route.query.tableId,
      };
      getAction(this.url.queryProjectBySampleId, params).then((res) => {
        if (res.success) {
          console.log(res.result);
          this.sheet = res.result.map((item) => {
            return {
              ...item,
              text: item.projectCode,
              value: item.id,
            };
          });
        }
      });
    },
    getSampleDetial() {
      let params = {
        id: this.$route.query.tableId,
      };
      getAction(this.url.samplelist, params).then((res) => {
        if (res.success) {
          console.log(this.$store.getters.userInfo);
          console.log(res.result);
          this.model = res.result;
          this.params.procDeTitle += res.result.sampleName;
          this.currentTime = new Date();
          this.model.returnDate = dayjs().format("YYYY-MM-DD");
          this.$set(
            this.model,
            "applyPerson",
            this.$store.getters.userInfo.realname
          );
          this.$set(
            this.model,
            "applyOrg",
            this.$store.getters.userInfo.orgCode
          );
          this.$set(
            this.model,
            "orgCodeTxt",
            this.$store.getters.userInfo.orgCodeTxt
          );
          // applyPersonTxt;
          this.$set(
            this.model,
            "applyPersonTxt",
            this.$store.getters.userInfo.realname
          );
        }
      });
    },
    getProcessNodeList() {
      let params = {
        processKey: this.$route.query.processKey,
        // tableId: this.$route.query.tableId,
      };
      getAction(this.url.ProcessNodeList, params).then((res) => {
        if (res.success) {
          this.params.processNodeList = res.result.slice(
            1,
            res.result.length - 1
          );
          this.params.assignees = res.result[1].assignees;
          console.log(res);
        }
      });
    },
    getDetail() {
      let params = {
        processKey: this.$route.query.processKey,
        // tableId: this.$route.query.tableId,
      };
      getAction(this.url.detail, params).then((res) => {
        if (res.success) {
          this.params.procDeTitle = `【${res.result.name}】`;
          this.params.procDefId = res.result.id;
          this.params.tableName = res.result.businessTable;
          console.log(res);
        }
      });
    },
  },
  components: {
    TabBar,
  },
};
</script>

<style lang="scss" scoped>
.van-button {
  width: 70%;
  border-radius: 5px;
}
</style>
